from common.mysql_operate import db

class FoodStore():
    def list(self):
        sql = "SELECT * FROM food"
        return db.select_db(sql)

    def queryByDiningHollId(self,id):
        sql = "SELECT * FROM `food` WHERE dininghall = {}".format(id)
        return db.select_db(sql)

    def selectDetailById(self,id):
        sql = "SELECT * FROM `food` WHERE id = {}".format(id)
        return db.select_db(sql)

    def selectBySearch(self,query):
        sql = "SELECT * FROM `food` WHERE food_name like '%{}%'".format(query)
        return db.select_db(sql)

    def getFoodType(self):
        sql = "SELECT DISTINCT type  FROM `food`"
        return db.select_db(sql)

    def getDingHall(self):
        sql = "SELECT DISTINCT dininghall FROM `food`"
        return db.select_db(sql)

    def addFood(self, food):
        sql = "INSERT INTO food VALUES(NULL,'{}','{}','{}','{}','{}','{}','{}','{}')".format(food.food_name, food.desc, food.create_time,
                                                                                             food.img_url, food.type, food.diningHall,
                                                                                             food.ingredient, food.remind)
        return db.execute_db(sql)

    def deleteFood(self,id):
        sql = "DELETE FROM food WHERE id = {}".format(id)
        return db.execute_db(sql)

    def updateFood(self,food):
        sql = "UPDATE food SET food_name='{}',`desc`='{}',img_url='{}',type='{}',diningHall='{}',ingredient='{}',remind='{}' WHERE id = {}".format(
            food.food_name,food.desc,food.img_url,food.type,food.diningHall,food.ingredient,food.remind,food.id
        )
        return db.execute_db(sql)

    def selectFoodByAuthorId(self,authorId):
        sql = "SELECT * FROM `blog` WHERE authorId ={}".format(authorId)
        return db.select_db(sql)

